import java.util.*;

public class HashTest {
    static public Scanner sc = new Scanner(System.in);

    // 简化printf输出语句
    static public void p(String format, Object ... obj) {
        System.out.printf(format, obj);
    }

    static public int [] getRange(int [] inputRange) {
        int [] range = {Integer.MIN_VALUE, Integer.MAX_VALUE};

        if (inputRange.length == 1) {
            range[1] = inputRange[0];
        } else if (inputRange.length > 1) {
            if (inputRange[0] == inputRange[1]) {
                range[0] = inputRange[0];
            } else if (inputRange[0] > inputRange[1]) {
                range[0] = inputRange[1];
                range[1] = inputRange[0];
            } else {
                range[0] = inputRange[0];
                range[1] = inputRange[1];
            }
        }

        return range;
    }

    // 获取随机数
    static public int rand(int ... inputRange) {
        int [] range = getRange(inputRange);

        range[0] = (range[0] < 0) ? 0 : range[0];
        range[1] = (range[1] == Integer.MAX_VALUE) ? range[1] - 1 : range[1];

        // p("start = %d, end = %d\n", range[0], range[1]);

        return (int)(Math.random() * (range[1] - range[0] + 1) + range[0]);
    }

    // 随机初始化数组
    static public int [] randArr(int [] arr, int len, int ... range) {
        int [] tmpArr = (arr == null) ? new int[len] : arr;
        int valCount = tmpArr.length > len ? len : tmpArr.length;

        for (int i = 0; i < valCount; ++i) {
            tmpArr[i] = rand(range);
        }

        return tmpArr;
    }

    // 输出数组
    static public int [] showArr(int [] arr, String prompt) {
        p(prompt);
        for (int i : arr) {
            p("%d ", i);
        }
        p("\n");

        return arr;
    }

    static public void main(String [] args) {
        int max = 100;
        int [] a = randArr(null, 10, 100);
        int index;

        showArr(a, "随机的数组: ");

        int [] hashTable = new int[max];
        for (int i = 0; i < a.length; ++i) {
            index = a[i] % max;
            hashTable[index] = a[i];
        }

        while (true) {
            p("请输入查找数组: ");
            int num = sc.nextInt();
            if (num == -1)
                break;

            p("find = %d\n", hashTable[num % max]);
        }
    }
}
